package com.lh.web.ctrl;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lh.biz.ICartBiz;
import com.lh.biz.dto.in.cart.AddItemInDto;
import com.lh.biz.dto.out.cart.AddItemOutDto;
import com.lh.ctrl.BaseCtrl;
import com.lh.entity.Cart;
import com.lh.exception.BizException;

/**
 * @author liuhui
 * @version 1.0
 * @date 2013-5-1 下午5:40:59
 */
@Controller
@RequestMapping("cart")
public class CartCtrl extends BaseCtrl {
	@Resource
	private ICartBiz cartBiz;
	
	@ResponseBody
	@RequestMapping("add-item")
	public AddItemOutDto addItem(@Valid AddItemInDto inDto   , HttpServletRequest request,HttpServletResponse response) throws BizException{
		String cartId = getCookieValue(request.getCookies(), Cart.CART_COOKIE_NAME);
		if(StringUtils.isNotBlank(cartId)){
			inDto.setCartId(Long.parseLong(cartId));
		}
		AddItemOutDto out =  cartBiz.addItem(inDto);
		if(out.isSuccess()){
			addCookie(response, Cart.CART_COOKIE_NAME, String.valueOf( out.getCartId() ), Cart.CART_COOKIE_AGE);
		}
		return out;
	}
	
}
